#! /bin/bash

# File:        npetoysrun
# Author:      Michael Rueger
# Last edited: 2005-02-28 18:42:20 by jens on impara.de
# Description: Script to start Etoys 
#              from the Browser Plugin (npetoys.so)

APPDIR=/usr/share/Etoys.app

BIN=$APPDIR/Contents/Linux-i686
VM=$BIN/etoys
VMOPTIONS="-encoding UTF-8 -plugins $BIN -vm-display-x11 "
IMAGE="$APPDIR/Contents/Resources/etoys.image"
IMOPTIONS=""
DOCUMENT=""
WRAPPER=""

# The (untrusted) user directory is used by etoys to store project files.
# Default is $HOME/Etoys

# The secure directory location is used to store per-user security information like the keys file.
# It needs to be outside the untrusted space.
# Default is $HOME/.etoys/Squeak.keys

[ -n "$ETOYS_SECUREDIR" ] && export SQUEAK_SECUREDIR="$ETOYS_SECUREDIR"
[ -n "$ETOYS_USERDIR" ] && export SQUEAK_USERDIR="$ETOYS_USERDIR"

[ -z "$SQUEAK_SECUREDIR" ] && export SQUEAK_SECUREDIR="$HOME/.etoys"
[ -z "$SQUEAK_USERDIR" ] && export SQUEAK_USERDIR="$HOME/Etoys"

[ ! -d "$SQUEAK_SECUREDIR" ] && mkdir -p -m 700 "$SQUEAK_SECUREDIR"
[ ! -d "$SQUEAK_USERDIR" ] && mkdir -p "$SQUEAK_USERDIR"

# is there a clever way to determine if you are local?
if [ $DISPLAY = ":0.0" ]; then
    VMOPTIONS=$VMOPTIONS" -xshm "
fi

# if pulseaudio is running, fall back to OSS
if pulseaudio --check 2>/dev/null ; then
    VMOPTIONS="$VMOPTIONS -vm-sound-oss"
    if padsp true 2>/dev/null ; then
        WRAPPER=padsp
    fi
fi

parameter_to_array(){
    i=0
    for p in "$@"; do
	i=$(( $i + 1 ))
	parameter[i]=$p
    done
}

parameter_to_array "$@"
parameter[6]=$IMAGE
$WRAPPER $VM $VMOPTIONS "${parameter[@]}"
